package com.example.maoyanmovie.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import androidx.appcompat.app.AppCompatActivity;
import com.example.maoyanmovie.databinding.ActivityLoginBinding;
import com.example.maoyanmovie.utils.Constants;
import com.example.maoyanmovie.utils.SPUtils;
import com.example.maoyanmovie.utils.ToastUtils;

public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initListeners();
    }

    private void initListeners() {
        binding.btnLogin.setOnClickListener(v -> login());

        binding.tvRegister.setOnClickListener(v -> {
            startActivity(new Intent(this, RegisterActivity.class));
        });
    }

    private void login() {
        String phone = binding.etPhone.getText().toString().trim();
        String password = binding.etPassword.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort(this, "请输入手机号");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort(this, "请输入密码");
            return;
        }

        if (phone.length() != 11) {
            ToastUtils.showShort(this, "手机号格式不正确");
            return;
        }

        // 模拟登录成功
        SPUtils.putBoolean(Constants.SP_KEY_IS_LOGIN, true);
        SPUtils.putString(Constants.SP_KEY_USER_PHONE, phone);
        SPUtils.putString(Constants.SP_KEY_USER_NAME, "用户" + phone.substring(7));

        ToastUtils.showShort(this, "登录成功");
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
